package com.xin.di.uav.applet.controller;

import com.xin.di.uav.applet.resp.PaymentBillResp;
import com.xin.di.uav.applet.service.ExportService;
import com.xin.di.uav.applet.service.PaymentBillService;
import com.xin.di.uav.common.utils.Rs;
import com.xin.di.uav.mapper.BillMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Date;


@RestController
@RequestMapping("/home/payment-bill/")
public class PaymentBillController {


    @Autowired
    private PaymentBillService paymentBillService;

    @Autowired
    private ExportService exportService;

    @Autowired
    private BillMapper billMapper;

    @GetMapping("list")
    public Rs<PaymentBillResp> list(@RequestParam int page, @RequestParam int pageSize, @RequestParam(required = false) String orderNo, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {

        page = (page - 1) * pageSize;
        PaymentBillResp paymentBillResp = new PaymentBillResp();
        int cnt = billMapper.count(orderNo, startTime, endTime);
        if (cnt > 0) {
            paymentBillResp.setTotal(cnt);
            paymentBillResp.setRecordList(paymentBillService.list(page, pageSize, orderNo, startTime, endTime));
        }


        return Rs.success(paymentBillResp);
    }


    @GetMapping("export")
    public Rs<Void> export(@RequestParam(required = false) String orderNo, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime
            , HttpServletResponse response) {
        exportService.export(orderNo, startTime, endTime, response);
        return Rs.success();
    }

}
